package com.yidiandao.business.ui.main.shoppingcart.chooseinvoice;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.BaseFragment;
import com.yidiandao.business.ui.main.shoppingcart.chooseinvoice.normal.ChooseNormalInvoicePresenter;
import com.yidiandao.business.ui.main.shoppingcart.chooseinvoice.normal.ChooseNormalInvoiceView;
import com.yidiandao.business.ui.main.shoppingcart.chooseinvoice.tax.ChooseTaxInvoicePresenter;
import com.yidiandao.business.ui.main.shoppingcart.chooseinvoice.tax.ChooseTaxInvoiceView;
import com.yidiandao.business.utils.KeyboardStateHelper;
import com.yidiandao.business.utils.KeyboardUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/**
 * 发票 View
 */
public class ChooseInvoiceFragment extends BaseFragment
        implements ChooseInvoiceContract.View, View.OnClickListener {

    private ChooseInvoiceContract.Presenter mPresenter;

    private ChooseTaxInvoicePresenter mTaxInvoicePresenter;

    private ChooseNormalInvoicePresenter mNormalInvoicePresenter;

    private Toolbar mToolbar;

    private TextView mTvNoInvoice;

    private TextView mTvNormalInvoice;

    private TextView mTvTaxInvoice;

    private Button mBtnConfirm;

    private ViewStub mNormalInvoiceStub;

    private View mNormalRoot;

    private ViewStub mTaxInvoiceStub;

    private View mTaxRoot;

    private ScrollView mScrollView;

    private RelativeLayout mBottomLayout;

    private KeyboardStateHelper mKeyboardStateHelper;

    public static ChooseInvoiceFragment getInstance(Bundle bundle) {
        ChooseInvoiceFragment fragment = new ChooseInvoiceFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void setPresenter(ChooseInvoiceContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return ChooseInvoiceFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    protected void initViews() {
        mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        mTvNoInvoice = (TextView) mRootView.findViewById(R.id.tv_no_invoice);
        mTvNormalInvoice = (TextView) mRootView.findViewById(R.id.tv_normal_invoice);
        mTvTaxInvoice = (TextView) mRootView.findViewById(R.id.tv_tax_invoice);
        mBtnConfirm = (Button) mRootView.findViewById(R.id.btn_confirm);
        mNormalInvoiceStub = (ViewStub) mRootView.findViewById(R.id.normal_invoice_stub);
        mTaxInvoiceStub = (ViewStub) mRootView.findViewById(R.id.tax_invoice_stub);
        mScrollView = (ScrollView) mRootView.findViewById(R.id.scroll_view);
        mBottomLayout = (RelativeLayout) mRootView.findViewById(R.id.bottom_layout);

        // 默认不开发票
        mPresenter.start();
        initKeyboardAbout();
        initListeners();
    }

    private void initKeyboardAbout() {
        mKeyboardStateHelper = new KeyboardStateHelper(mScrollView);
        mKeyboardStateHelper.setOnKeyboardStateChangeListener(new KeyboardStateHelper.OnKeyboardStateChangeListener() {
            @Override
            public void onKeyboardStateChange(boolean keyboardShow) {
                mBottomLayout.setVisibility(keyboardShow ? View.GONE : View.VISIBLE);
            }
        });
    }

    private void initListeners() {

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishActivity();
            }
        });

        // 不开发票
        mTvNoInvoice.setOnClickListener(this);
        // 普通发票
        mTvNormalInvoice.setOnClickListener(this);
        // 增值税发票
        mTvTaxInvoice.setOnClickListener(this);
        // 确认发票信息
        mBtnConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_no_invoice:
                changeInvoiceUIType(null);
                break;
            case R.id.tv_normal_invoice:
                changeInvoiceUIType(getNormalType());
                break;
            case R.id.tv_tax_invoice:
                changeInvoiceUIType(getTaxType());
                break;
            case R.id.btn_confirm:
                onClickConfirmButton();
                break;
        }
    }

    @Override
    public String getNormalType() {
        return mTvNormalInvoice.getText().toString();
    }

    @Override
    public String getTaxType() {
        return mTvTaxInvoice.getText().toString();
    }

    private void onClickConfirmButton() {

        // 确认选择发票
        if (TextUtils.isEmpty(mPresenter.getInvoiceType())) {
            mPresenter.onConfirmNoInvoice();
        } else if (mPresenter.getInvoiceType().equals(getNormalType())) {
            mNormalInvoicePresenter.onConfirmNormalInvoice();
        } else {
            mTaxInvoicePresenter.onConfirmTaxInvoice();
        }
    }

    @Override
    public void changeInvoiceUIType(String invoiceType) {
        KeyboardUtils.closeKeyboard(mActivity);

        // 保存当前选择的发票类型
        mPresenter.setInvoiceType(invoiceType);

        if (TextUtils.isEmpty(invoiceType)) {
            setNoInvoice();
        } else if (getNormalType().equals(invoiceType)) {
            setNormalInvoice();
        } else {
            setTaxInvoice();
        }
    }

    // 显示不需要发票UI及标记
    private void setNoInvoice() {
        mTvNoInvoice.setSelected(true);
        mTvNormalInvoice.setSelected(false);
        mTvTaxInvoice.setSelected(false);
        setNormalInvoiceLayoutVisibility(false);
        setTaxInvoiceLayoutVisibility(false);
    }

    // 显示普通发票UI及标记
    private void setNormalInvoice() {
        inflateNormalLayout();
        mTvNoInvoice.setSelected(false);
        mTvNormalInvoice.setSelected(true);
        mTvTaxInvoice.setSelected(false);
        setNormalInvoiceLayoutVisibility(true);
        setTaxInvoiceLayoutVisibility(false);
    }

    // 显示增值税发票UI及标记
    private void setTaxInvoice() {
        inflateTaxLayout();
        mTvNoInvoice.setSelected(false);
        mTvNormalInvoice.setSelected(false);
        mTvTaxInvoice.setSelected(true);
        setNormalInvoiceLayoutVisibility(false);
        setTaxInvoiceLayoutVisibility(true);
    }

    private void setNormalInvoiceLayoutVisibility(boolean visibility) {
        if (mNormalRoot != null) {
            mNormalRoot.setVisibility(visibility ? View.VISIBLE : View.GONE);
        }
    }

    private void setTaxInvoiceLayoutVisibility(boolean visibility) {
        if (mTaxRoot != null) {
            mTaxRoot.setVisibility(visibility ? View.VISIBLE : View.GONE);
        }
    }

    private void inflateTaxLayout() {
        if (mTaxRoot != null) {
            return;
        }

        mTaxRoot = mTaxInvoiceStub.inflate();
        // 选择普通发票的MVP
        ChooseTaxInvoiceView taxInvoiceView = new ChooseTaxInvoiceView(mTaxRoot, mActivity);
        mTaxInvoicePresenter = new ChooseTaxInvoicePresenter(getTaxType(),taxInvoiceView);
        mTaxInvoicePresenter.start();
    }

    private void inflateNormalLayout() {
        if (mNormalRoot != null) {
            return;
        }

        mNormalRoot = mNormalInvoiceStub.inflate();
        // 选择增值税发票的MVP
        ChooseNormalInvoiceView normalInvoiceView = new ChooseNormalInvoiceView(mNormalRoot, mActivity);
        mNormalInvoicePresenter = new ChooseNormalInvoicePresenter(normalInvoiceView);
        mNormalInvoicePresenter.start(getNormalType(), mPresenter.getDefaultNormalInvoice(), mPresenter.getNormalInvoiceList());
    }

    @Override
    public void finishActivity() {
        mActivity.finish();
    }

    @Override
    public void refreshInvoiceType(List<String> invoiceTypeList) {
        mTvNormalInvoice.setText(invoiceTypeList.get(0));
        mTvTaxInvoice.setText(invoiceTypeList.get(1));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mKeyboardStateHelper.unregisterAllListeners();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_choose_invoice;
    }
}
